class Api::V1::ImpactsController < Api::V1::BaseController

	prepend_before_filter :signed_in_user
	before_action :set_impact, only: [:show, :edit, :update, :destroy]


	def index
		@impacts = Impact.all
	end

	def create
		@impact = Impact.new(impact_params)
		if @impact.save
			render action: "show"
		else
			render :status => 406,
					:json => {
						:status_code => 406,
						:message => "Error creating resource!",
						:content => @impact.errors.full_messages
					}
		end
	end

	def show
	end

	def update
		if @impact.update(impact_params)
			render :status => 200,
					:json => { 
						:status_code => 200,
						:message => "Resource updated succesfully!",
						:content => ""
					 }
		else
			render :status => 406, 
					:json => {
						:status_code => 406,
						:message => "Error updating resource!"
						:content => @impact.errors.full_messages
					 }
		end
	end

	def destroy
		if @impact.destroy
			render :status => 200,
					:json => { 
						:status_code => 200,
						:message => "Resource deleted succesfully!",
						:content => ""
					}
		else
			render :status => 406, 
					:json => {
						:status_code => 406,
						:message => "Error deleting resource!",
						:content => @impact.errors.full_messages
					}
		end
	end

	private

		def set_impact
     		@impacts = Impact.find(params[:id])
     		rescue ActiveRecord::RecordNotFound
				error = "The resource that you were looking for could not be found."
				render :status => 404, 
						:json => { 
							:status_code => 404,
							:message => error,
							:content => ""
						 }
   		end

		def impact_params
			params.require(:impact).permit(:decision_id, :force_id, :description)
		end
end
